/*******************************************************************************
 *     ___                  _   ____  ____
 *    / _ \ _   _  ___  ___| |_|  _ \| __ )
 *   | | | | | | |/ _ \/ __| __| | | |  _ \
 *   | |_| | |_| |  __/\__ \ |_| |_| | |_) |
 *    \__\_\\__,_|\___||___/\__|____/|____/
 *
 *  Copyright (c) 2014-2019 Appsicle
 *  Copyright (c) 2019-2024 QuestDB
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 ******************************************************************************/

package io.questdb.test.cutlass.http;

import io.questdb.cairo.CairoConfiguration;
import io.questdb.cairo.DefaultCairoConfiguration;
import io.questdb.cairo.arr.ArrayTypeDriver;
import io.questdb.cairo.arr.ArrayWriteState;
import io.questdb.cairo.arr.DirectArray;
import io.questdb.cairo.arr.NoopArrayWriteState;
import io.questdb.std.datetime.MicrosecondClock;
import io.questdb.std.datetime.NanosecondClock;
import io.questdb.std.str.CharSink;
import io.questdb.std.str.StringSink;
import io.questdb.std.str.Utf16Sink;
import io.questdb.test.AbstractTest;
import io.questdb.test.tools.TestUtils;
import org.jetbrains.annotations.NotNull;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;

public class ArrayBufferOverflowTest extends AbstractTest {
    private static CairoConfiguration configuration;

    @BeforeClass
    public static void setUpStatic() throws Exception {
        AbstractTest.setUpStatic();
        configuration = new DefaultCairoConfiguration(root) {
            @Override
            public @NotNull MicrosecondClock getMicrosecondClock() {
                // this fixes the random seeds used by rnd_array()
                return () -> 1234L;
            }

            @Override
            public NanosecondClock getNanosecondClock() {
                return () -> 5678L;
            }
        };
    }

    @Test
    public void testArrayToTextContinuity() {
        try (DirectArray arrayView = new DirectArray(configuration)) {
            var rnd = TestUtils.generateRandom(LOG);
            rnd.nextDoubleArray(Math.max(1, rnd.nextInt(4)), arrayView, 1, 10, 0);
            var sinkActual = new StringSink();
            var sinkExpected = new StringSink();
            // we do not split values yet, such as double, so the sink should be
            // large enough for at least 1 value.
            var chunkSize = Math.max(31, rnd.nextInt(10000));
            var sinkInterrupted = new Utf16Sink() {
                private boolean bufOut = true;

                @Override
                public Utf16Sink put(char c) {
                    if (sinkActual.length() > 0 && (sinkActual.length() % chunkSize) == 0) {
                        if (bufOut) {
                            // interrupt the sinking on each chunk
                            // when we reach the chunk, we expect the code to retry to climb over
                            // this chunk, and continue, but we will fail again on the next chunk
                            bufOut = false;
                            throw new RuntimeException("buf out");
                        } else {
                            bufOut = true;
                        }
                    }
                    return sinkActual.put(c);
                }
            };

            var statePrototype = new ArrayWriteState() {
                int opsAlreadyDone;
                int opsSeenSinceRestart;
                int sinkLen = 0;

                @Override
                public boolean incAndSayIfNewOp() {
                    opsSeenSinceRestart++;
                    return opsSeenSinceRestart > opsAlreadyDone;
                }

                @Override
                public void performedOp() {
                    opsAlreadyDone = Math.max(opsAlreadyDone, opsSeenSinceRestart);
                    sinkLen = sinkActual.length();
                }

                @Override
                public void putCharIfNew(CharSink<?> sink, char symbol) {
                    if (incAndSayIfNewOp()) {
                        sink.put(symbol);
                    }
                    performedOp();
                }
            };

            while (true) {
                try {
                    ArrayTypeDriver.arrayToJson(
                            arrayView,
                            sinkInterrupted,
                            ArrayTypeDriver::appendDoubleFromArrayToSink,
                            statePrototype
                    );
                    break;
                } catch (Throwable e) {
                    sinkActual.trimTo(statePrototype.sinkLen);
                    statePrototype.opsSeenSinceRestart = 0;
                }
            }

            ArrayTypeDriver.arrayToJson(
                    arrayView,
                    sinkExpected,
                    ArrayTypeDriver::appendDoubleFromArrayToSink,
                    NoopArrayWriteState.INSTANCE
            );

            TestUtils.assertEquals(sinkExpected, sinkActual);
        }
    }

    @Test
    public void testJsonHttp() throws Exception {
        var rnd = TestUtils.generateRandom(LOG);
        TestUtils.assertMemoryLeak(() -> {
            HttpServerConfigurationBuilder httpServerConfigurationBuilder = new HttpServerConfigurationBuilder();
            httpServerConfigurationBuilder.withSendBufferSize(Math.max(384, rnd.nextInt(2048)));
            new HttpQueryTestBuilder()
                    .withTempFolder(root)
                    .withWorkerCount(1)
                    .withHttpServerConfigBuilder(httpServerConfigurationBuilder)
                    .withTelemetry(false).run(configuration, (engine, sqlExecutionContext) -> {
                        try (TestHttpClient testHttpClient = new TestHttpClient()) {
                            testHttpClient.assertGet(
                                    "{\"query\":\"select x i, rnd_double_array(3, 2, 0, 6, 5, 5) a from long_sequence(10);\",\"columns\":[{\"name\":\"i\",\"type\":\"LONG\"},{\"name\":\"a\",\"type\":\"ARRAY\",\"dim\":3,\"elemType\":\"DOUBLE\"}],\"timestamp\":-1,\"dataset\":[[1,[[[null,0.5003140121745173,null,null,0.19698875078723133],[null,0.7572578154632928,null,0.08560822536426049,null],[null,0.33945278041414917,0.5750125140140324,0.3430187550362488,null],[null,0.29480980135158996,0.6479345173546692,null,0.2045062590399923],[0.6127856476566909,0.8503942441500951,null,null,null]],[[0.019147693920114173,null,0.4906599400807965,null,0.3968083307152436],[0.15660021358970966,0.6646972311522397,0.9585902415056963,0.6655104192004756,null],[null,0.1172746514867844,null,0.6511045047182841,0.7683105406339571],[0.9290614071514778,0.19769207844601266,0.9195280110369135,null,0.9749259769443541],[null,0.9625377422988265,0.023354979752552962,0.23057591369498254,null]],[[null,null,null,0.10900094032561602,0.04647192376115372],[null,null,null,0.1766381214088677,null],[0.5453977271934628,null,null,null,null],[0.7851540286356862,0.5415303553955547,null,0.29977049469413086,null],[null,0.7606635696353011,null,0.17254810746886884,null]],[[null,null,0.10487124002456405,0.4186300625386815,0.5017871743292444],[0.3731713745336884,0.6930108509272134,null,0.41105652588260766,0.7407135240631251],[null,null,0.09484850139180045,0.7585034880444966,0.6903404130663208],[0.41185423773561725,0.366235036291611,0.7389681993911548,0.6463190161004667,0.6260869679487912],[0.7863148894013694,0.8052974926672505,null,null,0.41917037592222084]],[[0.23076991425250926,null,0.07368475256381368,null,0.18975893506734198],[null,0.115053567698442,null,0.7379862763120206,0.6875714732223362],[null,0.8455664228442337,null,null,0.402103179842381],[0.39914436875193016,null,0.6723478124856548,null,0.4713032196899246],[0.2523947553878946,null,null,null,0.6674930639338373]],[[null,0.9376954983887946,0.1313725436598021,null,0.8596081257999777],[0.8946111847541557,0.6950797436427438,null,null,0.32236202632180977],[0.5952581305625118,0.9487756757370154,0.029781720764282427,null,null],[null,null,0.299194230836316,0.39788407698477435,null],[null,null,0.46765086062296124,0.8350586479928622,null]]]],[2,[[[null,0.1835698685511945,null,0.5375473896789547,null],[null,0.39019583593893425,null,0.7591275489144484,0.8706567086224487],[null,0.5856408092648538,0.9151918987110386,null,0.7896934320010015],[null,null,0.824135545788026,0.6730183595300052,0.13264971242897128],[0.4185213858031468,0.06901354659239123,0.7122459035203224,0.4677383549098585,0.09969400014614915]],[[0.15874654594597115,0.8004681039724987,0.1071476173910979,null,null],[0.09655049392113946,null,null,0.7294042062973564,0.5657820746737012],[null,null,0.9873376564511632,0.9706702204330769,0.18942088085860465],[0.7823345302045017,null,0.12591661010872168,0.7844149290300105,null],[0.49651315459049405,null,0.9810108766267983,null,null]],[[0.1350250753376553,null,0.7096202671078397,0.43395334268992847,0.6166301999053899],[null,null,0.31079637376489677,0.2773988044002518,null],[0.5630302499452692,0.8736241889752498,0.7670816959845673,0.19996414487896885,null],[null,null,0.3909058216265646,null,0.19921852803187778],[0.8951533842889456,0.08440863304152058,0.5204100681015866,null,0.28177025652045007]],[[0.5892665487636375,null,0.26765040430780496,0.1570201096546444,null],[0.11043091151502193,null,null,null,null],[null,null,null,null,0.6689981981121359],[0.4831388469520921,0.20921072125771667,null,0.5816964059343437,null],[0.4307043869825331,0.22236075590380688,null,null,0.018964840883960332]],[[null,0.2486540411674587,0.6472333044933509,null,0.40524311980271877],[null,null,null,0.590207457800767,0.9873776577169371],[null,0.37212462606961905,null,0.647708926929469,0.7993695281095538],[null,null,0.47278190470524406,null,0.15709499387870274],[0.29041296071895606,null,null,null,null]],[[0.8105893733141054,0.7021654932059425,null,null,null],[null,0.27413711518772244,null,0.9648524426635225,0.567100008030412],[0.9043515089122987,0.566290461939287,null,0.7198102710689762,0.36548545213161476],[0.026737125834976005,null,null,null,null],[0.18623373100345675,0.45276991641784026,null,0.7911579296107376,null]]]],[3,[[[0.09700228519402843,0.13160774678571296,0.5275425690603268,null,0.8836470131224086],[null,0.014798332523307645,0.2497455769026249,0.30104723674587386,null],[0.32358936750558,0.49545164091363425,0.6070164103056869,null,null],[0.23755234745548925,0.5494977406488426,null,0.8475518374699751,0.6856944657376532],[null,0.6098896987846916,null,null,0.6772144837288119]],[[null,0.9617529185247894,0.16029480944964125,0.3379157510611779,0.37656517275177026],[0.27026554481353326,0.9475724622167355,null,null,0.0951143596682067],[0.7713656984996624,null,null,0.18674617411769168,null],[0.6393724886725091,0.8294009251733827,0.9352843594526411,null,0.8656169212925849],[null,0.09541131051834917,null,null,0.06772273666845885]],[[null,0.4160966811647834,0.8795762760519621,null,0.3955232553485767],[null,0.9926071135364998,null,null,null],[0.19139262409307378,0.4780279443956511,0.4361614337390951,0.304763513985835,0.34117480010616275],[0.08697165996698775,0.9452363374262904,null,null,0.15943741882293816],[0.11469003872484684,null,null,0.167646298981327,0.7037535727329766]],[[null,0.07595565736920673,null,0.6822560578770913,null],[null,null,0.3340396067136411,null,0.635253567710728],[null,0.5789368382442128,null,null,null],[null,0.2618590277755881,null,0.40287343024846467,null],[null,null,null,null,null]],[[0.3818989709303825,0.7571468297856421,null,0.021210092687211857,null],[null,0.3083512969992941,null,null,null],[0.7670449725353162,null,null,0.17690588451044797,0.04085594739172127],[0.42963802976167564,0.15406737016497507,null,0.27598687305767866,0.616397961332087],[0.03015392301707609,null,0.5811195658057996,0.1687739216531623,0.30794195412830494]],[[null,0.4985016826334143,null,null,0.8297395306733835],[null,null,0.751961330622461,0.9278154892339202,2.877924760262829E-4],[null,null,null,0.005506092417727815,0.9717957796361866],[0.532286109237085,0.9582310144100555,0.2505772405396218,null,0.02817870350903673],[null,null,0.2369865020562376,null,0.6393235921711882]]]],[4,[[[0.07798165211317931,null,0.05770063669961378,0.49721814723655244,null],[null,null,null,0.469311432886398,0.44289569573751586],[0.03657057264111274,null,null,0.7271819243600804,null],[0.9319944399663949,null,null,null,0.16959210263380342],[null,0.5846197909563031,null,0.389938073624028,null]],[[0.5410668591965017,0.04394335649771275,null,0.3295120267970686,null],[null,null,0.7256977609321453,null,null],[null,0.4579658392602012,null,null,null],[0.719805129577167,0.8995233511787948,0.6095594038019319,0.5182198546790386,null],[null,null,null,null,0.3103146247681887]],[[null,0.08757090353605002,null,null,0.6397402886759618],[null,0.2350672851590423,0.39011156911342637,0.15626901898244772,null],[0.8253330925690316,null,0.8715175933522069,0.12613745482212468,0.8830763604433802],[0.15918123361244585,0.9734952037184823,0.4370470066777422,null,null],[0.781092690297811,0.05011365198841222,null,0.5363324085511425,0.6830821503086657]],[[null,0.11005308685152315,0.3748467565263852,null,null],[0.39770741485481453,null,0.6488100101729691,0.7029054778621999,null],[null,null,null,0.5355313960160839,null],[0.7318132665994841,null,0.554962197263144,0.863326984530121,0.8858375623817673],[null,null,0.21195542400409995,0.9602152573427033,null]],[[null,0.9236452060883239,null,0.5336634379965686,null],[null,0.34874446849725693,null,0.5376669006346759,null],[null,0.7741009255757904,null,0.8269580324715078,0.21306740278820768],[0.8800745566204461,null,null,null,0.1634542626815002],[0.48646012028534824,0.5710409359890984,null,null,null]],[[null,0.05089202990692565,null,null,null],[0.45886444876714116,null,0.1748187037536535,null,0.8181746522590782],[null,null,0.5890707176435558,null,null],[null,0.9561918047709165,0.30531348622266463,null,null],[0.5681918088113993,0.14638131023723644,0.9381660002291241,null,0.580115864796304]]]],[5,[[[0.1870717737970905,null,0.8205025340212999,0.19744637814021693,0.8953466102969829],[null,null,null,null,0.18391888174080284],[null,null,null,0.23486161828226282,0.22690808972133847],[null,0.5983084886397971,null,0.5045140951065086,0.30328438168819893],[0.6277672256038669,0.020677904174344897,null,0.25782026077272835,null]],[[null,0.07479679644665549,0.8945050526607826,null,null],[0.6794418747477671,0.8577686584313057,0.4162560393308651,0.8919044167929883,null],[0.3407962886005309,0.04052462618382957,null,null,0.0713485574911672],[null,0.795947207306414,0.8209144418467785,0.10684540848568014,0.34389031748746746],[null,null,0.9914671463090217,null,null]],[[0.8379893290927722,0.20507891834134528,0.6948330746978721,0.04550534412675844,0.14041801332011172],[0.34905267084741787,0.27644142693465557,null,null,0.007741470436727127],[0.12426511442732846,null,null,0.40573654448789753,0.340492500273189],[null,0.22883052525300285,0.6796242134531126,0.6135936677355017,0.23579481672899083],[null,null,0.23870826185360627,null,null]],[[0.0056800321560877,0.3478010888671986,null,null,null],[null,null,null,0.9321443671715673,null],[0.6972141260607075,null,null,0.7371654264459363,null],[null,0.8454332267721028,0.5727465129720001,null,0.1504363732629922],[null,null,0.8789422249903721,0.5662716128661945,null]],[[null,0.699975431211048,0.9075264980878782,null,0.6761740198041265],[null,null,null,null,null],[null,null,0.9660886063686286,null,0.4981991848570205],[0.7797686681144637,0.052533983860752365,null,null,0.2710946569004825],[0.4701570116036756,0.8926576720290728,0.6694671428186679,0.5071434119167283,null]],[[0.6118342148796497,0.2658974150258555,0.2807796203695444,0.9382670896985986,0.4378160719418541],[null,0.35475518167848985,null,null,0.19482303624025454],[0.33022503520439883,null,0.8959570867495006,0.5826875467178274,null],[0.45480397663535266,0.23625946698790523,null,null,null],[0.07608623677859916,0.008350458192965537,0.9430249935222196,0.6635705922432534,0.20122661444196277]]]],[6,[[[null,null,null,null,null],[0.8332539113474976,null,null,0.9840272070761341,0.16898570825063586],[null,0.9285994390593608,0.4046838758042345,0.12091645051422117,0.46067972974102267],[0.6984943252944391,null,null,null,0.5592940519418743],[0.8667069510492704,null,null,0.9819044645522805,0.8274870843532716]],[[null,0.1149592993713634,0.01425114105181624,null,null],[0.3452108611978083,null,null,0.5810528017878991,0.7483572945142705],[0.2713349384553817,0.4338987033693271,0.9778812911850688,0.6221844119801614,0.6326465578667374],[0.6554664497893651,0.9680411783102004,0.671408576833102,null,null],[null,0.9739165878770795,0.9660201435039061,null,null]],[[null,0.9427018051221101,0.25807353220609053,0.2907502312360456,0.43301593346633094],[0.8095469911770462,0.2094979547234398,null,null,0.7331280002498503],[null,0.192620785823848,null,0.7037462664503981,0.45439353299474794],[0.3849499013012102,null,null,0.7750160370660677,0.4276038769652569],[null,null,null,null,null]],[[0.09269568115821736,0.575900644956335,null,0.629704965329424,0.3130234564146763],[0.6669629925759253,0.4804830594574423,null,null,null],[null,null,null,0.4214221685451538,null],[null,null,null,null,0.6651860463468472],[0.622451612132452,null,null,null,null]],[[null,0.07491622471647408,null,null,0.134483826003864],[0.2890440222865347,0.527557966067841,0.9604366995728939,0.7411837442807394,0.864684219461405],[0.7579515307206305,0.2943585644877912,0.8504599323664312,0.19286011543870796,null],[0.4224195401628611,null,null,null,0.8264759333341277],[null,0.7642076519169563,null,null,null]],[[null,null,0.009847617855426982,null,0.9053148732938472],[0.6037456229930546,0.44622410043638083,0.17567597158049086,0.9265317544136137,null],[0.2984834334519457,null,0.11813474534614443,0.4164482904027057,0.17572445660661473],[null,null,0.8092655669867447,null,0.44713033157197224],[0.2814420921578351,0.47181982168079317,null,null,null]]]],[7,[[[null,0.229099559977322,null,null,null],[0.9091618722948607,0.40976026498124296,null,0.7414917316321218,null],[null,0.26736806566531857,0.043909843481076294,null,0.1001459612084521],[null,0.8718328102199935,null,0.16359451541371295,null],[null,null,0.2498618811464941,null,null]],[[null,null,null,0.1041093764299823,null],[0.9797019218387812,null,0.5692328631414538,0.63199185914977,0.8539840469749463],[0.6352427751531645,null,0.3980409668425797,0.16283044525330692,0.6956399720900903],[null,null,0.007231918256693226,0.5174558493460827,0.94402323657743],[0.9410083885199868,0.193509732435445,null,0.7002130081041986,null]],[[0.9076813603257569,null,null,0.5571600614605219,0.30433458114945455],[null,0.2433723615878688,null,null,null],[null,null,null,null,0.44298448347761565],[0.7624145655729776,null,null,null,0.5871503634517878],[null,0.9663791881759272,0.6087134731467403,0.3369855730953625,null]],[[null,null,0.10266563869254952,0.014806439922342318,null],[0.40478636174776694,0.32413708267374153,null,null,null],[0.8621257935549713,null,null,null,0.07299083736134138],[null,null,0.13940093331056658,0.5593991694859953,null],[0.4138299037782387,0.7594071755796881,0.39611656375689364,0.09889978907883878,null]],[[0.19155731280055288,0.6091583225003531,null,0.06742569574338675,0.10557576180131056],[0.5994881744165074,null,0.9109196259759144,null,null],[0.8248357126611113,null,0.24116138061489312,null,null],[0.8792079713879042,null,0.5388390469770816,null,0.6571572670152026],[null,null,null,0.9492121844944159,null]],[[0.1979844715900565,null,0.01364180167566309,null,null],[null,null,null,null,0.9968262736965298],[0.2585801377898064,null,null,0.6899833455473111,0.8751946558153301],[null,null,null,null,0.03874166618786934],[null,0.20562866349972453,null,null,null]]]],[8,[[[null,null,0.6705637037463261,null,null],[0.9556347397572135,null,0.7231729545140543,null,null],[null,null,0.5786908350236659,null,null],[0.8921377309677689,null,null,0.3722244279039071,null],[null,null,0.06161591568106117,null,0.7595870106621233]],[[0.22257597801369078,0.6935133170364117,0.9267887065273117,null,null],[0.05804255417867776,null,0.5464868271213721,0.23067957309731157,0.20720899747438848],[null,0.3578335482131745,0.6099178655509291,null,null],[0.15930340025863843,0.5820002431627987,null,null,0.7526152560293169],[0.8131329094617634,null,null,null,null]],[[null,null,null,null,0.868317254382104],[null,null,0.11154308209960173,0.027003736607679962,null],[null,0.7850843484143221,0.09685524484186314,0.03592827404748333,0.1296056309955238],[0.6332857418024572,0.5847142958769441,0.4963119027238654,null,null],[0.6029142092653902,null,0.09055381090925407,0.48293261497495976,0.06425116491204608]],[[null,0.18530347113949852,null,0.6328410553487422,null],[null,null,null,0.3869474936026158,null],[0.13150968560472875,null,null,0.2863535365080532,null],[null,null,null,null,0.41020360391942456],[null,0.37292048739939754,null,null,null]],[[null,null,0.6536312880359604,null,0.8347646895810054],[null,null,0.5228482469397542,null,null],[0.9878993455423041,0.1738919408887939,0.755904284233798,null,null],[null,0.19433268456118946,0.0871724149879265,0.8053415581033504,0.775761031979181],[0.7696798949240472,null,0.5679707193335644,0.10718089698699862,0.9600014020966862]],[[null,0.8393155641944526,0.996056180822628,null,0.3837106184615827],[0.26259255263972914,0.4861701904479312,0.7143149264859477,0.6047458094794833,null],[0.7716379531029901,null,0.9080410905811653,0.8948180564432152,0.16429982758519046],[0.5555892169133388,0.5164710418555739,0.40848679556312106,null,0.7308515975179171],[0.09731174826908828,0.6347328500004975,0.7025764616715118,null,0.3515813471008119]]]],[9,[[[0.6535207911297289,null,null,null,0.2845684469128862],[0.6244286387558226,null,0.9771144448483805,0.046169309865663255,null],[0.9161124007558968,0.1653745586314992,null,0.9540324458133191,0.9822418849158586],[null,null,0.945297557759027,0.29860905858140796,null],[0.27134736371127555,0.3326653203960812,0.5921096379735478,0.7374634040488053,0.23383340043448275]],[[0.688095568350634,0.407215042196997,0.46069558990101256,0.6825836820491478,0.19414990973498136],[null,0.04453151854859849,null,0.37306835405364813,0.032754233189361104],[null,0.862573468818104,0.36706820539725515,0.5333240899035981,0.552986125383901],[0.215171091331829,null,0.29519206650176144,0.4942079506455256,0.5820387756290786],[null,null,0.16596514892659597,0.04051678859627139,null]],[[null,null,null,null,0.6020642475499534],[null,null,0.7013458969431272,null,null],[null,0.7332404266781918,0.45727842168348976,null,null],[0.8628944697893186,null,0.9197266025801707,0.8546084925559261,null],[0.8578364380521102,0.9276480823795574,0.6970452582572932,null,0.4064684855153331]],[[0.1448291352667186,null,null,null,0.356328813172082],[null,null,null,null,0.021011946397660797],[0.7463898206340907,0.6763034428849729,null,null,null],[null,0.7766737832230426,0.2719779299457715,null,0.8018444176448448],[null,null,null,null,0.02754465862764255]],[[null,null,0.9310595843653257,null,0.2243399441463345],[0.2374589254237447,0.23914231027739574,0.6191694981702018,0.2230817991690235,0.41299257344047713],[0.8279120438220678,null,null,0.0245696596629148,0.6432702341672304],[0.6620561169384709,null,0.4337818813159091,0.027999634733084866,0.38359478871942654],[null,0.6035447373027908,null,null,0.9751247434335504]],[[null,0.5655573310084958,null,null,0.375018298194849],[0.002306364970716124,null,0.6541401855195669,0.062222095730324556,null],[null,null,0.7397919871558706,null,null],[0.4293386548287744,null,null,null,0.43025004892259266],[0.6154697914893327,0.264451659153488,null,0.08177669886699912,0.3471928074944619]]]],[10,[[[null,null,0.38672457623575196,0.3660730376546546,0.9355946372288765],[0.6046334526411014,null,0.44416900742010124,0.2943888276610489,0.3241899243410059],[null,0.6732880982717181,null,0.21388134557824723,0.06838640276926666],[0.8261231849594167,null,null,null,null],[null,0.09772751815127179,null,null,0.4423257558615884]],[[0.9982708390116904,null,0.6403058675050838,null,null],[0.23767247201421815,null,0.7897529167101084,null,null],[0.7571054893122693,0.8502971471085463,null,null,0.061791577143295306],[0.5244925582368088,null,0.18326916479274524,0.6093348787232759,0.8850648295396407],[0.04009569550548431,0.12781811413376032,0.22943421064660174,null,0.46918557581060827]],[[0.4739083662702267,0.018003307843214023,0.05490881138268067,0.8094285082096924,null],[null,0.1380396062613931,0.998907140671013,null,null],[null,null,0.5656285481729845,null,null],[0.44386840137425476,null,0.7421646665937606,null,null],[0.43052267000428246,null,null,0.6326609717579553,0.02981322175252854]],[[0.25372275220781215,null,0.33417938845291717,0.6076083103145163,0.17727889804334374],[null,null,null,0.7897782711548227,null],[null,0.9765516314339352,null,null,null],[null,null,0.5249305067944606,0.40820106523933375,null],[null,0.8540415661726509,null,null,null]],[[0.08751782277632258,null,0.6398141612557999,0.631951965470677,null],[null,0.7788351247434359,null,0.7163276652946697,0.47701113201232137],[0.5646357829691336,null,0.9193837401789761,null,null],[0.6039047121021297,0.3725138949465816,null,null,0.5613434422971743],[0.5228553016998089,null,0.2011451718175452,null,null]],[[0.4200383573163773,0.508427798838802,null,0.31952936502977614,0.34496146029374497],[0.7014774045376598,null,0.5304739523376752,null,null],[null,null,0.16067647241417993,0.43885674017373344,0.2805568793530232],[null,null,null,null,0.05103459290813572],[0.9932366303195541,null,0.2929772140016663,null,0.9204074851827915]]]]],\"count\":10}",
                                    // It is important that the array is not the first column, there is ',' state that has to be dealt with.
                                    // It is also important that other columns in the select statement are not "rnd", this is due to the
                                    // fact that column value is re-requested from the record in case it could not be sent due to
                                    // send buffer being full. Array values are cached in the state, they are not re-requested from the record,
                                    // whereas every primitive value is re-requested, and it moves the random seeds.
                                    "select x i, rnd_double_array(3, 2, 0, 6, 5, 5) a from long_sequence(10);"
                            );
                        }
                    });
        });
    }

    @Test
    public void testTextHttp() throws Exception {
        var rnd = TestUtils.generateRandom(LOG);
        TestUtils.assertMemoryLeak(() -> {
            HttpServerConfigurationBuilder httpServerConfigurationBuilder = new HttpServerConfigurationBuilder();
            httpServerConfigurationBuilder.withSendBufferSize(Math.max(384, rnd.nextInt(2048)));
            new HttpQueryTestBuilder()
                    .withTempFolder(root)
                    .withWorkerCount(1)
                    .withHttpServerConfigBuilder(httpServerConfigurationBuilder)
                    .withTelemetry(false).run(configuration, (engine, sqlExecutionContext) -> {
                        try (TestHttpClient testHttpClient = new TestHttpClient()) {
                            // It is important that the array is not the first column, there is ',' state that has to be
                            // dealt with. It is also important that other columns in the select statement are not
                            // "rnd", this is due to the fact that column value is re-requested from the record in case
                            // it could not be sent due to send buffer being full. Array values are cached in the state,
                            // they are not re-requested from the record, whereas every primitive value is re-requested,
                            // and it moves the random seeds.
                            testHttpClient.assertGet(
                                    "/exp",
                                    "\"i\",\"a\"\r\n" +
                                            "1,\"[[[null,0.5003140121745173,null,null,0.19698875078723133],[null,0.7572578154632928,null,0.08560822536426049,null],[null,0.33945278041414917,0.5750125140140324,0.3430187550362488,null],[null,0.29480980135158996,0.6479345173546692,null,0.2045062590399923],[0.6127856476566909,0.8503942441500951,null,null,null]],[[0.019147693920114173,null,0.4906599400807965,null,0.3968083307152436],[0.15660021358970966,0.6646972311522397,0.9585902415056963,0.6655104192004756,null],[null,0.1172746514867844,null,0.6511045047182841,0.7683105406339571],[0.9290614071514778,0.19769207844601266,0.9195280110369135,null,0.9749259769443541],[null,0.9625377422988265,0.023354979752552962,0.23057591369498254,null]],[[null,null,null,0.10900094032561602,0.04647192376115372],[null,null,null,0.1766381214088677,null],[0.5453977271934628,null,null,null,null],[0.7851540286356862,0.5415303553955547,null,0.29977049469413086,null],[null,0.7606635696353011,null,0.17254810746886884,null]],[[null,null,0.10487124002456405,0.4186300625386815,0.5017871743292444],[0.3731713745336884,0.6930108509272134,null,0.41105652588260766,0.7407135240631251],[null,null,0.09484850139180045,0.7585034880444966,0.6903404130663208],[0.41185423773561725,0.366235036291611,0.7389681993911548,0.6463190161004667,0.6260869679487912],[0.7863148894013694,0.8052974926672505,null,null,0.41917037592222084]],[[0.23076991425250926,null,0.07368475256381368,null,0.18975893506734198],[null,0.115053567698442,null,0.7379862763120206,0.6875714732223362],[null,0.8455664228442337,null,null,0.402103179842381],[0.39914436875193016,null,0.6723478124856548,null,0.4713032196899246],[0.2523947553878946,null,null,null,0.6674930639338373]],[[null,0.9376954983887946,0.1313725436598021,null,0.8596081257999777],[0.8946111847541557,0.6950797436427438,null,null,0.32236202632180977],[0.5952581305625118,0.9487756757370154,0.029781720764282427,null,null],[null,null,0.299194230836316,0.39788407698477435,null],[null,null,0.46765086062296124,0.8350586479928622,null]]]\"\r\n" +
                                            "2,\"[[[null,0.1835698685511945,null,0.5375473896789547,null],[null,0.39019583593893425,null,0.7591275489144484,0.8706567086224487],[null,0.5856408092648538,0.9151918987110386,null,0.7896934320010015],[null,null,0.824135545788026,0.6730183595300052,0.13264971242897128],[0.4185213858031468,0.06901354659239123,0.7122459035203224,0.4677383549098585,0.09969400014614915]],[[0.15874654594597115,0.8004681039724987,0.1071476173910979,null,null],[0.09655049392113946,null,null,0.7294042062973564,0.5657820746737012],[null,null,0.9873376564511632,0.9706702204330769,0.18942088085860465],[0.7823345302045017,null,0.12591661010872168,0.7844149290300105,null],[0.49651315459049405,null,0.9810108766267983,null,null]],[[0.1350250753376553,null,0.7096202671078397,0.43395334268992847,0.6166301999053899],[null,null,0.31079637376489677,0.2773988044002518,null],[0.5630302499452692,0.8736241889752498,0.7670816959845673,0.19996414487896885,null],[null,null,0.3909058216265646,null,0.19921852803187778],[0.8951533842889456,0.08440863304152058,0.5204100681015866,null,0.28177025652045007]],[[0.5892665487636375,null,0.26765040430780496,0.1570201096546444,null],[0.11043091151502193,null,null,null,null],[null,null,null,null,0.6689981981121359],[0.4831388469520921,0.20921072125771667,null,0.5816964059343437,null],[0.4307043869825331,0.22236075590380688,null,null,0.018964840883960332]],[[null,0.2486540411674587,0.6472333044933509,null,0.40524311980271877],[null,null,null,0.590207457800767,0.9873776577169371],[null,0.37212462606961905,null,0.647708926929469,0.7993695281095538],[null,null,0.47278190470524406,null,0.15709499387870274],[0.29041296071895606,null,null,null,null]],[[0.8105893733141054,0.7021654932059425,null,null,null],[null,0.27413711518772244,null,0.9648524426635225,0.567100008030412],[0.9043515089122987,0.566290461939287,null,0.7198102710689762,0.36548545213161476],[0.026737125834976005,null,null,null,null],[0.18623373100345675,0.45276991641784026,null,0.7911579296107376,null]]]\"\r\n" +
                                            "3,\"[[[0.09700228519402843,0.13160774678571296,0.5275425690603268,null,0.8836470131224086],[null,0.014798332523307645,0.2497455769026249,0.30104723674587386,null],[0.32358936750558,0.49545164091363425,0.6070164103056869,null,null],[0.23755234745548925,0.5494977406488426,null,0.8475518374699751,0.6856944657376532],[null,0.6098896987846916,null,null,0.6772144837288119]],[[null,0.9617529185247894,0.16029480944964125,0.3379157510611779,0.37656517275177026],[0.27026554481353326,0.9475724622167355,null,null,0.0951143596682067],[0.7713656984996624,null,null,0.18674617411769168,null],[0.6393724886725091,0.8294009251733827,0.9352843594526411,null,0.8656169212925849],[null,0.09541131051834917,null,null,0.06772273666845885]],[[null,0.4160966811647834,0.8795762760519621,null,0.3955232553485767],[null,0.9926071135364998,null,null,null],[0.19139262409307378,0.4780279443956511,0.4361614337390951,0.304763513985835,0.34117480010616275],[0.08697165996698775,0.9452363374262904,null,null,0.15943741882293816],[0.11469003872484684,null,null,0.167646298981327,0.7037535727329766]],[[null,0.07595565736920673,null,0.6822560578770913,null],[null,null,0.3340396067136411,null,0.635253567710728],[null,0.5789368382442128,null,null,null],[null,0.2618590277755881,null,0.40287343024846467,null],[null,null,null,null,null]],[[0.3818989709303825,0.7571468297856421,null,0.021210092687211857,null],[null,0.3083512969992941,null,null,null],[0.7670449725353162,null,null,0.17690588451044797,0.04085594739172127],[0.42963802976167564,0.15406737016497507,null,0.27598687305767866,0.616397961332087],[0.03015392301707609,null,0.5811195658057996,0.1687739216531623,0.30794195412830494]],[[null,0.4985016826334143,null,null,0.8297395306733835],[null,null,0.751961330622461,0.9278154892339202,2.877924760262829E-4],[null,null,null,0.005506092417727815,0.9717957796361866],[0.532286109237085,0.9582310144100555,0.2505772405396218,null,0.02817870350903673],[null,null,0.2369865020562376,null,0.6393235921711882]]]\"\r\n" +
                                            "4,\"[[[0.07798165211317931,null,0.05770063669961378,0.49721814723655244,null],[null,null,null,0.469311432886398,0.44289569573751586],[0.03657057264111274,null,null,0.7271819243600804,null],[0.9319944399663949,null,null,null,0.16959210263380342],[null,0.5846197909563031,null,0.389938073624028,null]],[[0.5410668591965017,0.04394335649771275,null,0.3295120267970686,null],[null,null,0.7256977609321453,null,null],[null,0.4579658392602012,null,null,null],[0.719805129577167,0.8995233511787948,0.6095594038019319,0.5182198546790386,null],[null,null,null,null,0.3103146247681887]],[[null,0.08757090353605002,null,null,0.6397402886759618],[null,0.2350672851590423,0.39011156911342637,0.15626901898244772,null],[0.8253330925690316,null,0.8715175933522069,0.12613745482212468,0.8830763604433802],[0.15918123361244585,0.9734952037184823,0.4370470066777422,null,null],[0.781092690297811,0.05011365198841222,null,0.5363324085511425,0.6830821503086657]],[[null,0.11005308685152315,0.3748467565263852,null,null],[0.39770741485481453,null,0.6488100101729691,0.7029054778621999,null],[null,null,null,0.5355313960160839,null],[0.7318132665994841,null,0.554962197263144,0.863326984530121,0.8858375623817673],[null,null,0.21195542400409995,0.9602152573427033,null]],[[null,0.9236452060883239,null,0.5336634379965686,null],[null,0.34874446849725693,null,0.5376669006346759,null],[null,0.7741009255757904,null,0.8269580324715078,0.21306740278820768],[0.8800745566204461,null,null,null,0.1634542626815002],[0.48646012028534824,0.5710409359890984,null,null,null]],[[null,0.05089202990692565,null,null,null],[0.45886444876714116,null,0.1748187037536535,null,0.8181746522590782],[null,null,0.5890707176435558,null,null],[null,0.9561918047709165,0.30531348622266463,null,null],[0.5681918088113993,0.14638131023723644,0.9381660002291241,null,0.580115864796304]]]\"\r\n" +
                                            "5,\"[[[0.1870717737970905,null,0.8205025340212999,0.19744637814021693,0.8953466102969829],[null,null,null,null,0.18391888174080284],[null,null,null,0.23486161828226282,0.22690808972133847],[null,0.5983084886397971,null,0.5045140951065086,0.30328438168819893],[0.6277672256038669,0.020677904174344897,null,0.25782026077272835,null]],[[null,0.07479679644665549,0.8945050526607826,null,null],[0.6794418747477671,0.8577686584313057,0.4162560393308651,0.8919044167929883,null],[0.3407962886005309,0.04052462618382957,null,null,0.0713485574911672],[null,0.795947207306414,0.8209144418467785,0.10684540848568014,0.34389031748746746],[null,null,0.9914671463090217,null,null]],[[0.8379893290927722,0.20507891834134528,0.6948330746978721,0.04550534412675844,0.14041801332011172],[0.34905267084741787,0.27644142693465557,null,null,0.007741470436727127],[0.12426511442732846,null,null,0.40573654448789753,0.340492500273189],[null,0.22883052525300285,0.6796242134531126,0.6135936677355017,0.23579481672899083],[null,null,0.23870826185360627,null,null]],[[0.0056800321560877,0.3478010888671986,null,null,null],[null,null,null,0.9321443671715673,null],[0.6972141260607075,null,null,0.7371654264459363,null],[null,0.8454332267721028,0.5727465129720001,null,0.1504363732629922],[null,null,0.8789422249903721,0.5662716128661945,null]],[[null,0.699975431211048,0.9075264980878782,null,0.6761740198041265],[null,null,null,null,null],[null,null,0.9660886063686286,null,0.4981991848570205],[0.7797686681144637,0.052533983860752365,null,null,0.2710946569004825],[0.4701570116036756,0.8926576720290728,0.6694671428186679,0.5071434119167283,null]],[[0.6118342148796497,0.2658974150258555,0.2807796203695444,0.9382670896985986,0.4378160719418541],[null,0.35475518167848985,null,null,0.19482303624025454],[0.33022503520439883,null,0.8959570867495006,0.5826875467178274,null],[0.45480397663535266,0.23625946698790523,null,null,null],[0.07608623677859916,0.008350458192965537,0.9430249935222196,0.6635705922432534,0.20122661444196277]]]\"\r\n" +
                                            "6,\"[[[null,null,null,null,null],[0.8332539113474976,null,null,0.9840272070761341,0.16898570825063586],[null,0.9285994390593608,0.4046838758042345,0.12091645051422117,0.46067972974102267],[0.6984943252944391,null,null,null,0.5592940519418743],[0.8667069510492704,null,null,0.9819044645522805,0.8274870843532716]],[[null,0.1149592993713634,0.01425114105181624,null,null],[0.3452108611978083,null,null,0.5810528017878991,0.7483572945142705],[0.2713349384553817,0.4338987033693271,0.9778812911850688,0.6221844119801614,0.6326465578667374],[0.6554664497893651,0.9680411783102004,0.671408576833102,null,null],[null,0.9739165878770795,0.9660201435039061,null,null]],[[null,0.9427018051221101,0.25807353220609053,0.2907502312360456,0.43301593346633094],[0.8095469911770462,0.2094979547234398,null,null,0.7331280002498503],[null,0.192620785823848,null,0.7037462664503981,0.45439353299474794],[0.3849499013012102,null,null,0.7750160370660677,0.4276038769652569],[null,null,null,null,null]],[[0.09269568115821736,0.575900644956335,null,0.629704965329424,0.3130234564146763],[0.6669629925759253,0.4804830594574423,null,null,null],[null,null,null,0.4214221685451538,null],[null,null,null,null,0.6651860463468472],[0.622451612132452,null,null,null,null]],[[null,0.07491622471647408,null,null,0.134483826003864],[0.2890440222865347,0.527557966067841,0.9604366995728939,0.7411837442807394,0.864684219461405],[0.7579515307206305,0.2943585644877912,0.8504599323664312,0.19286011543870796,null],[0.4224195401628611,null,null,null,0.8264759333341277],[null,0.7642076519169563,null,null,null]],[[null,null,0.009847617855426982,null,0.9053148732938472],[0.6037456229930546,0.44622410043638083,0.17567597158049086,0.9265317544136137,null],[0.2984834334519457,null,0.11813474534614443,0.4164482904027057,0.17572445660661473],[null,null,0.8092655669867447,null,0.44713033157197224],[0.2814420921578351,0.47181982168079317,null,null,null]]]\"\r\n" +
                                            "7,\"[[[null,0.229099559977322,null,null,null],[0.9091618722948607,0.40976026498124296,null,0.7414917316321218,null],[null,0.26736806566531857,0.043909843481076294,null,0.1001459612084521],[null,0.8718328102199935,null,0.16359451541371295,null],[null,null,0.2498618811464941,null,null]],[[null,null,null,0.1041093764299823,null],[0.9797019218387812,null,0.5692328631414538,0.63199185914977,0.8539840469749463],[0.6352427751531645,null,0.3980409668425797,0.16283044525330692,0.6956399720900903],[null,null,0.007231918256693226,0.5174558493460827,0.94402323657743],[0.9410083885199868,0.193509732435445,null,0.7002130081041986,null]],[[0.9076813603257569,null,null,0.5571600614605219,0.30433458114945455],[null,0.2433723615878688,null,null,null],[null,null,null,null,0.44298448347761565],[0.7624145655729776,null,null,null,0.5871503634517878],[null,0.9663791881759272,0.6087134731467403,0.3369855730953625,null]],[[null,null,0.10266563869254952,0.014806439922342318,null],[0.40478636174776694,0.32413708267374153,null,null,null],[0.8621257935549713,null,null,null,0.07299083736134138],[null,null,0.13940093331056658,0.5593991694859953,null],[0.4138299037782387,0.7594071755796881,0.39611656375689364,0.09889978907883878,null]],[[0.19155731280055288,0.6091583225003531,null,0.06742569574338675,0.10557576180131056],[0.5994881744165074,null,0.9109196259759144,null,null],[0.8248357126611113,null,0.24116138061489312,null,null],[0.8792079713879042,null,0.5388390469770816,null,0.6571572670152026],[null,null,null,0.9492121844944159,null]],[[0.1979844715900565,null,0.01364180167566309,null,null],[null,null,null,null,0.9968262736965298],[0.2585801377898064,null,null,0.6899833455473111,0.8751946558153301],[null,null,null,null,0.03874166618786934],[null,0.20562866349972453,null,null,null]]]\"\r\n" +
                                            "8,\"[[[null,null,0.6705637037463261,null,null],[0.9556347397572135,null,0.7231729545140543,null,null],[null,null,0.5786908350236659,null,null],[0.8921377309677689,null,null,0.3722244279039071,null],[null,null,0.06161591568106117,null,0.7595870106621233]],[[0.22257597801369078,0.6935133170364117,0.9267887065273117,null,null],[0.05804255417867776,null,0.5464868271213721,0.23067957309731157,0.20720899747438848],[null,0.3578335482131745,0.6099178655509291,null,null],[0.15930340025863843,0.5820002431627987,null,null,0.7526152560293169],[0.8131329094617634,null,null,null,null]],[[null,null,null,null,0.868317254382104],[null,null,0.11154308209960173,0.027003736607679962,null],[null,0.7850843484143221,0.09685524484186314,0.03592827404748333,0.1296056309955238],[0.6332857418024572,0.5847142958769441,0.4963119027238654,null,null],[0.6029142092653902,null,0.09055381090925407,0.48293261497495976,0.06425116491204608]],[[null,0.18530347113949852,null,0.6328410553487422,null],[null,null,null,0.3869474936026158,null],[0.13150968560472875,null,null,0.2863535365080532,null],[null,null,null,null,0.41020360391942456],[null,0.37292048739939754,null,null,null]],[[null,null,0.6536312880359604,null,0.8347646895810054],[null,null,0.5228482469397542,null,null],[0.9878993455423041,0.1738919408887939,0.755904284233798,null,null],[null,0.19433268456118946,0.0871724149879265,0.8053415581033504,0.775761031979181],[0.7696798949240472,null,0.5679707193335644,0.10718089698699862,0.9600014020966862]],[[null,0.8393155641944526,0.996056180822628,null,0.3837106184615827],[0.26259255263972914,0.4861701904479312,0.7143149264859477,0.6047458094794833,null],[0.7716379531029901,null,0.9080410905811653,0.8948180564432152,0.16429982758519046],[0.5555892169133388,0.5164710418555739,0.40848679556312106,null,0.7308515975179171],[0.09731174826908828,0.6347328500004975,0.7025764616715118,null,0.3515813471008119]]]\"\r\n" +
                                            "9,\"[[[0.6535207911297289,null,null,null,0.2845684469128862],[0.6244286387558226,null,0.9771144448483805,0.046169309865663255,null],[0.9161124007558968,0.1653745586314992,null,0.9540324458133191,0.9822418849158586],[null,null,0.945297557759027,0.29860905858140796,null],[0.27134736371127555,0.3326653203960812,0.5921096379735478,0.7374634040488053,0.23383340043448275]],[[0.688095568350634,0.407215042196997,0.46069558990101256,0.6825836820491478,0.19414990973498136],[null,0.04453151854859849,null,0.37306835405364813,0.032754233189361104],[null,0.862573468818104,0.36706820539725515,0.5333240899035981,0.552986125383901],[0.215171091331829,null,0.29519206650176144,0.4942079506455256,0.5820387756290786],[null,null,0.16596514892659597,0.04051678859627139,null]],[[null,null,null,null,0.6020642475499534],[null,null,0.7013458969431272,null,null],[null,0.7332404266781918,0.45727842168348976,null,null],[0.8628944697893186,null,0.9197266025801707,0.8546084925559261,null],[0.8578364380521102,0.9276480823795574,0.6970452582572932,null,0.4064684855153331]],[[0.1448291352667186,null,null,null,0.356328813172082],[null,null,null,null,0.021011946397660797],[0.7463898206340907,0.6763034428849729,null,null,null],[null,0.7766737832230426,0.2719779299457715,null,0.8018444176448448],[null,null,null,null,0.02754465862764255]],[[null,null,0.9310595843653257,null,0.2243399441463345],[0.2374589254237447,0.23914231027739574,0.6191694981702018,0.2230817991690235,0.41299257344047713],[0.8279120438220678,null,null,0.0245696596629148,0.6432702341672304],[0.6620561169384709,null,0.4337818813159091,0.027999634733084866,0.38359478871942654],[null,0.6035447373027908,null,null,0.9751247434335504]],[[null,0.5655573310084958,null,null,0.375018298194849],[0.002306364970716124,null,0.6541401855195669,0.062222095730324556,null],[null,null,0.7397919871558706,null,null],[0.4293386548287744,null,null,null,0.43025004892259266],[0.6154697914893327,0.264451659153488,null,0.08177669886699912,0.3471928074944619]]]\"\r\n" +
                                            "10,\"[[[null,null,0.38672457623575196,0.3660730376546546,0.9355946372288765],[0.6046334526411014,null,0.44416900742010124,0.2943888276610489,0.3241899243410059],[null,0.6732880982717181,null,0.21388134557824723,0.06838640276926666],[0.8261231849594167,null,null,null,null],[null,0.09772751815127179,null,null,0.4423257558615884]],[[0.9982708390116904,null,0.6403058675050838,null,null],[0.23767247201421815,null,0.7897529167101084,null,null],[0.7571054893122693,0.8502971471085463,null,null,0.061791577143295306],[0.5244925582368088,null,0.18326916479274524,0.6093348787232759,0.8850648295396407],[0.04009569550548431,0.12781811413376032,0.22943421064660174,null,0.46918557581060827]],[[0.4739083662702267,0.018003307843214023,0.05490881138268067,0.8094285082096924,null],[null,0.1380396062613931,0.998907140671013,null,null],[null,null,0.5656285481729845,null,null],[0.44386840137425476,null,0.7421646665937606,null,null],[0.43052267000428246,null,null,0.6326609717579553,0.02981322175252854]],[[0.25372275220781215,null,0.33417938845291717,0.6076083103145163,0.17727889804334374],[null,null,null,0.7897782711548227,null],[null,0.9765516314339352,null,null,null],[null,null,0.5249305067944606,0.40820106523933375,null],[null,0.8540415661726509,null,null,null]],[[0.08751782277632258,null,0.6398141612557999,0.631951965470677,null],[null,0.7788351247434359,null,0.7163276652946697,0.47701113201232137],[0.5646357829691336,null,0.9193837401789761,null,null],[0.6039047121021297,0.3725138949465816,null,null,0.5613434422971743],[0.5228553016998089,null,0.2011451718175452,null,null]],[[0.4200383573163773,0.508427798838802,null,0.31952936502977614,0.34496146029374497],[0.7014774045376598,null,0.5304739523376752,null,null],[null,null,0.16067647241417993,0.43885674017373344,0.2805568793530232],[null,null,null,null,0.05103459290813572],[0.9932366303195541,null,0.2929772140016663,null,0.9204074851827915]]]\"\r\n",
                                    "select x i, rnd_double_array(3, 2, 0, 6, 5, 5) a from long_sequence(10);",
                                    null,
                                    null
                            );
                        }
                    });
        });
    }

    @Ignore
    @Test
    public void testTextHttpWithNonVanilla() throws Exception {
        var rnd = TestUtils.generateRandom(LOG);
        TestUtils.assertMemoryLeak(() -> {
            HttpServerConfigurationBuilder httpServerConfigurationBuilder = new HttpServerConfigurationBuilder();
            httpServerConfigurationBuilder.withSendBufferSize(Math.max(384, rnd.nextInt(2048)));
            new HttpQueryTestBuilder()
                    .withTempFolder(root)
                    .withWorkerCount(1)
                    .withHttpServerConfigBuilder(httpServerConfigurationBuilder)
                    .withTelemetry(false).run(configuration, (engine, sqlExecutionContext) -> {
                        try (TestHttpClient testHttpClient = new TestHttpClient()) {
                            testHttpClient.assertGet(
                                    "/exp",
                                    "arr\n[[1.0,3.0,5.0],[2.0,4.0,6.0]]",
                                    "select transpose(ARRAY[[1.0,2],[3,4],[5,6]]) arr from long_sequence(1)"
                            );
                        }
                    });
        });
    }
}
